﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Configuration;
using Newtonsoft.Json;

namespace PromotionEngine
{
    public partial class PromotionCustomerInclude : System.Web.UI.Page
    {
        public class ResponseDetail
        {
            public List<Model.PromotionCustomerInclude> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseDetail(List<Model.PromotionCustomerInclude> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseDetail(string errors)
            {
                this.Errors = errors;
            }

            public ResponseDetail()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string propertyID = Request.QueryString["PromotionID"];
            string propertyCode = Request.QueryString["PromotionCode"];
            string propertyName = Request.QueryString["PromotionName"];

            this.lblPromotionCode.Text = propertyCode;
            this.lblPromotionName.Text = propertyName;
            this.lblPromotionID.Value = propertyID;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetCustomerInclude(string promotionID)
        {
            if (promotionID!=null && promotionID.Length > 0)
            {
                List<Model.PromotionCustomerInclude> lstPromotionCustomerIn = new List<Model.PromotionCustomerInclude>();
                Promotion pmt = new Promotion();
                DataSet dsPromotion = pmt.GetAllPromotionCustomerInByPromotionID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), promotionID);
                if (dsPromotion != null)
                {
                    foreach (DataRow r in dsPromotion.Tables[0].Rows)
                    {
                        Model.PromotionCustomerInclude promotionCustomerIn = new Model.PromotionCustomerInclude();
                        promotionCustomerIn.LineID = r["LineID"].ToString();
                        promotionCustomerIn.PromotionID = r["PromotionID"].ToString();
                        promotionCustomerIn.SourceType = r["SourceType"].ToString();
                        promotionCustomerIn.SourceValue = r["SourceValue"].ToString();
                        promotionCustomerIn.ReferenceName = r["ReferenceName"].ToString();
                        promotionCustomerIn.DiscountCode = r["DiscountCode"].ToString();
                        promotionCustomerIn.SourceValueName = r["SourceValueName"].ToString();
                        lstPromotionCustomerIn.Add(promotionCustomerIn);
                    }
                    var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstPromotionCustomerIn };
                    return new ResponseDetail(lstPromotionCustomerIn, dsPromotion.Tables[0].Rows.Count);
                }
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateCustomerInclude(string oPromotionCustomerInclude, bool isInsert)
        {
            Model.PromotionCustomerInclude promotionCustomerIn = JsonConvert.DeserializeObject<Model.PromotionCustomerInclude>(oPromotionCustomerInclude);

            if (promotionCustomerIn != null)
            {
                if (promotionCustomerIn.SourceType == "3")
                {
                    promotionCustomerIn.SourceValue = promotionCustomerIn.SourceValueName;
                }
                Promotion pmt = new Promotion();
                string errMsg = pmt.UpdateJsonPromotionCustomerIn(promotionCustomerIn.LineID, promotionCustomerIn.PromotionID, promotionCustomerIn.SourceType
                                                        , promotionCustomerIn.SourceValue, promotionCustomerIn.DiscountCode,
                                                        ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
                if (errMsg.Length == 0)
                {
                    //Show error message
                }
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteCustomerInclude(string lineID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonPromotionCustomerIn(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), lineID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllReferenceValueSourceType()
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), "CustomerSourceType");
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);

            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetCustomerPropertys()
        {
            List<Model.CustomerProperty> lstCustomerProperty = new List<Model.CustomerProperty>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCustomerPropertyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {
                DataTable tbMerge = new DataTable();
                tbMerge.Columns.Add("SourceValue");
                tbMerge.Columns.Add("SourceValueName");
                tbMerge.Columns.Add("ReferenceValue");
                if (dsPromotion.Tables.Count > 0)
                {
                    foreach (DataRow r in dsPromotion.Tables[0].Rows)
                    {
                        DataRow row = tbMerge.NewRow();
                        row["SourceValue"] = r["PropertyDetailID"].ToString();
                        row["SourceValueName"] = r["PropertyDetailName"].ToString();
                        row["ReferenceValue"] = "2";
                        tbMerge.Rows.Add(row);
                    }
                }
                dsPromotion = pmt.GetAllCustomer(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                 bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
                if (dsPromotion != null)
                {
                    if (dsPromotion.Tables.Count > 0)
                    {
                        foreach (DataRow r in dsPromotion.Tables[0].Rows)
                        {
                            DataRow row = tbMerge.NewRow();
                            row["SourceValue"] = r["CustomerID"].ToString();
                            row["SourceValueName"] = r["CustomerName"].ToString();
                            row["ReferenceValue"] = "1";
                            tbMerge.Rows.Add(row);
                        }
                    }
                }
                return JsonConvert.SerializeObject(tbMerge);
            }
            return string.Empty;
        }
    }
}